<?php

class kTabs {
    var $cnt=0;
    var $tabs=array();
    var $sub=array();
    var $divID=NULL;
    var $LIclass=NULL;
    var $activePage=Null;
    var $mainTab=0;


    function addTab($name,$link,$sub=NULL) {
        $cnt=$this->cnt;
        $w['name']=$name;
        $w['link']=$link;
        if (isset($sub)) {
            $w['sub']=$sub;
        }
        $this->tabs[$cnt]=$w;
        $this->cnt += 1;
    }

// build HTML LI
    function buildLI($name,$link) {
        if ($this->currLI ($link)) {
            $w=$this->currentLI_html($name,$link);
        } else {
            $w=$this->normalLI_html($name,$link);
        }
        return $w;
    }
//-------------------*/

    function normalLI_html($name,$link) {
        $w='<li><a href="'.$link.'">'.$name.'</a></li>';
        return $w;
    }
    function currentLI_html($name,$link) {
        $w='<li><span>'.$name.'</span></li>';
        return $w;
    }


// check if the current page is active.
    function currLI ($link) {
        if ($this->activePage==$link) {
            return true;
        } else {
            return false;
        }
    }

    function render () {
        $tst=$_SERVER["REQUEST_URI"];
        $base=basename($tst);
        $remove_this= strstr(basename($tst),'?');
        //$tst2=str_replace($remove_this,'',$tst); // not sure if this necessary

        $this->activePage=basename($base);

        $w='<div id="kTab"><ul id="primary">';
        foreach ($this->tabs as $tab) {
//Has subtab?
//-------------------------------
            $this->mainTab=0;
            if (isset($tab['sub'])) {  //Has subtab?: yes
                $c=count ($tab['sub']);

                $sub='<ul id="secondary">';
                foreach ($tab['sub'] as $subtab) { //each subtab loop
// Is subtab current?
                    $sub .=$this->buildLI($subtab['name'],$subtab['link']);
                    if ($this->currLI ($subtab['link'])) $this->mainTab=1;
                }
                $sub .='</ul>';
                if ($this->mainTab==1) { // check if this main tab is current
                    $w .= $this->currentLI_html($tab['name'],$tab['link']);
                    $w .= $sub;
                } else {
                    $w .= $this->normalLI_html($tab['name'],$tab['link']);
                }
            } else { //Has subtab?:NO
                $w .= $this->buildLI($tab['name'],$tab['link']);
            }
        }
        $w .='</ul></div>';
        return $w;
    }


}
?>